using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace LPV_POS.mh_pos.mh_cauhinh
{
    public partial class mh_phongchu : DevExpress.XtraEditors.XtraForm
    {
        DataTable tb;
        private string phongchu = "";
        private string cochu = "20";
        Color mauchu;
        Font _font;
        private DataTable f_dsfont()
        {
            tb = new DataTable();
            tb.Columns.Add("tenfont", typeof(string));

            System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
            foreach (FontFamily family in fonts.Families)
                tb.Rows.Add(family.Name);
            return tb;
        }
        private DataTable f_dsfont(string sFontName)
        {
            tb = new DataTable();
            tb.Columns.Add("tenfont", typeof(string));

            System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
            foreach (FontFamily family in fonts.Families)
            {
                if (family.Name.ToLower().IndexOf(sFontName.ToLower()) == 0)
                    tb.Rows.Add(family.Name);
            }
            return tb;
        }
        public mh_phongchu()
        {
            InitializeComponent();
        }
        public mh_phongchu(string phongchu, string cochu, Color mauchu)
        {
            InitializeComponent();
            this.phongchu = phongchu;
            this.cochu = cochu;
            this.mauchu = mauchu;
        }

        private void mh_phongchu_Load(object sender, EventArgs e)
        {
            txt_cochu.Text =this.cochu ;
            lbel_mauchu.BackColor = this.mauchu;
            luoi_font.MainView = grid_font;
            luoi_font.DataSource = null;
            luoi_font.DataSource = f_dsfont();
            txt_cochu.Text = lbel_view.Font.Size.ToString();
            DataView dv = (DataView)grid_font.DataSource;
            dv.Sort = "tenfont";
            int index = dv.Find(this.phongchu);
            if (index > 0)
            {
                grid_font.FocusedRowHandle = index;
                grid_font.SelectRow(index);
              
            }

            return;
 
        }

        private void grid_font_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (grid_font.RowCount > 0)
                {
                    string font_name = grid_font.GetRowCellValue(grid_font.FocusedRowHandle, col_font).ToString();
                    lbel_view.Text = font_name;

                    lbel_view.Font = new Font(font_name.Replace(".", ""), float.Parse(txt_cochu.Text));
                }
            }
            catch (Exception)
            {
                return;
            }
            
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Variable.int_close = -1;
            this.Close();
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void bton_tang_Click(object sender, EventArgs e)
        {
            int size = int.Parse(txt_cochu.Text) +1;
            txt_cochu.Text = size.ToString();
            lbel_view.Font = new Font(lbel_view.Font.FontFamily, size);
        }

      
        private void bton_giam_Click(object sender, EventArgs e)
        {
            if (txt_cochu.Text == "0")
                return;
            int size = int.Parse(txt_cochu.Text) - 1;
            txt_cochu.Text = size.ToString();
            lbel_view.Font = new Font(lbel_view.Font.FontFamily, size);
        }

     


        private void lbel_mauchu_Click(object sender, EventArgs e)
        {
            Variable.str_chu_nen = "nen";
            mh_color frm_color = new mh_color();
            frm_color.ShowDialog();
            lbel_mauchu.BackColor = Variable.maunen;
        }

        private void lbel_mauchu_BackColorChanged(object sender, EventArgs e)
        {
            lbel_view.ForeColor = lbel_mauchu.BackColor;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            try
            {
                _font = new Font(lbel_view.Text.Replace(".", ""), float.Parse(txt_cochu.Text));

            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            Variable.int_close = 1;
            this.Close();
        }

        public Font ShowForm_Font()
        {
            //chi hien thi FONT -- ko hien thi COLOR
            lblmauchu.Visible = false;
            lbel_mauchu.Visible = false;
            //mauchu = Color.Black;
            this.ShowDialog();

            return _font;
        }

        private void btnFont_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
            string s= omh_chu.ShowForm(txtFont.Text, 200, 200, false);
            if (omh_chu.DialogResult == DialogResult.OK)
                txtFont.Text = s;
        }

        private void txtFont_TextChanged(object sender, EventArgs e)
        {
            luoi_font.MainView = grid_font;
            luoi_font.DataSource = null;
            luoi_font.DataSource = this.f_dsfont(txtFont.Text.Trim());
        }

        private void grid_font_Click(object sender, EventArgs e)
        {
            this.grid_font_FocusedRowChanged(null, null);
        }
    }
}